package com.taptech.doufu.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ListView;

import com.taptech.doufu.bean.ImageBean;

/**
 * Created by lsy on 2015/6/29.
 */
public class ListviewInScrollView extends ListView
{

    public ListviewInScrollView(Context context)
    {
        super(context);
    }

    public ListviewInScrollView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ListviewInScrollView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }


    public void setLayoutHeight(int height)
    {
        ViewGroup.LayoutParams listViewParams = (ViewGroup.LayoutParams) this.getLayoutParams();
        listViewParams.height = height;
        this.requestLayout();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        return super.onTouchEvent(event);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

    public int calculateHeight(ImageBean[] imageBeans, int screenWidht)
    {
        int totalHeight = 0;
        for (ImageBean imageBean : imageBeans)
        {
            float scale = screenWidht / (float) Integer.valueOf(imageBean.getThumbnail_width());
            totalHeight += (Integer.valueOf(imageBean.getThumbnail_height()) * scale);
        }
        return totalHeight;

    }

    public boolean isAtTop()
    {
        return getScrollY() <= 0;
    }


    public boolean isUp(int y, int oldY)
    {

        return y > oldY;
    }

    public boolean isDown(int y, int oldY)
    {

        return y < oldY;
    }

    public boolean isAtBottom()
    {
        return getScrollY() >= getChildAt(getChildCount() - 1).getBottom() + getPaddingBottom() - getHeight();
    }
}
